﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace LabTurtorial
{
    public partial class Lab05 : Form
    {
        public Lab05()
        {
            InitializeComponent();
            
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            Stream stream;            
            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                stream = openDialog.OpenFile();
                if (stream != null)
                {
                    string filePath = openDialog.FileName;
                    try
                    {
                        string content = File.ReadAllText(filePath);
                        rtbContent.Text = content;
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            DialogResult result = colorDialog.ShowDialog();
            if (result == DialogResult.OK) {
                if (rtbContent.SelectedText.Length > 0)
                {
                    rtbContent.SelectionColor = colorDialog.Color;
                }
                else {
                    rtbContent.SelectAll();
                    rtbContent.SelectionColor = colorDialog.Color;
                }
                rtbContent.Select(rtbContent.TextLength,rtbContent.TextLength);
                rtbContent.Focus();
            }
        }

        private void btnFont_Click(object sender, EventArgs e)
        {
            DialogResult result = fontDialog.ShowDialog();
            if (result == DialogResult.OK) {
                if (rtbContent.SelectedText.Length > 0)
                {
                    rtbContent.SelectionFont = fontDialog.Font;
                }
                else {
                    rtbContent.SelectAll();
                    rtbContent.Font = fontDialog.Font;
                }
                rtbContent.Focus();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Do you want to save file?", "Message Box", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                
                string filename = "lab05.txt";                
                FileStream file = File.Open(filename, FileMode.Create);
                using (StreamWriter sw = new StreamWriter(file))
                {
                    sw.Write(rtbContent.Text);
                    
                    MessageBox.Show("Successful", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                this.Dispose();
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            DialogResult result = printDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                MessageBox.Show("Print Successfull","Message Box",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else {
                MessageBox.Show("Print Failure", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnPageSetup_Click(object sender, EventArgs e)
        {
            pageSetupDialog.Document = printDocument;
            pageSetupDialog.ShowDialog();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            printPreviewDialog.ShowDialog();
        }

        private void employeeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Lab02 dialog = new Lab02();
            dialog.TopLevel = false;
            dialog.MdiParent = this;
            
            TabPage newPage = new TabPage();
            newPage.Text = dialog.Text;
            newPage.Controls.Add(dialog);
            dialog.FormBorderStyle = FormBorderStyle.None;
            dialog.Dock = DockStyle.Fill;
            tabControl.TabPages.Add(newPage);
            dialog.Visible = true;
        }
    }
}
